#include "common/assert.h"
#include "common/exception.h"
#include "win32/lean_windows.h"
#include "win32/throw_win32_exception.h"

#include <cstdio>

namespace
{
	// If this compiles, then Lean_Windows.h works.
	void Test_Lean_Windows()
	{
		MessageBoxA(
			0,
			"Hello world!",
			"Box",
			0);
	}

	void Test_Throw_Win32_Exception()
	{
		bool create_threw = false;

		try
		{
			// Try to open an inexistent file
			HANDLE result = CreateFileA(
				"INVALIDXXX",
				FILE_READ_DATA | FILE_WRITE_DATA,
				FILE_SHARE_WRITE,
				0,
				OPEN_EXISTING,
				0,
				0);

			if (result == INVALID_HANDLE_VALUE)
			{
				Throw_Win32_Exception("CreateFileA");
			}
		}
		catch (const Exception& e)
		{
			std::printf(
				"CreateFileA(\"INVALIDXXX\") resulted in exception "
				"as expected:\n\n%s",
				e.message());

			create_threw = true;
		}

		if (!create_threw)
			throw Exception("Throw_Win32_Exception() didn't throw!");

		SetLastError(ERROR_SUCCESS);

		// Should not throw
		Throw_Win32_Exception("No error");
	}
}

int win32_test()
{
	try
	{
		Test_Throw_Win32_Exception();
		Test_Lean_Windows();
	} catch (const Exception& exception)
	{
		std::printf(exception.message());
		getchar();
	}

	return 0;
}